package org.eenie.cartoonist.ui.book;

import android.support.annotation.NonNull;

import org.eenie.cartoonist.api.book.BookApi;
import org.eenie.cartoonist.model.BookDetailEntity;

import javax.inject.Inject;

import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Eenie on 2016/8/18 13:15.
 * Des：
 */

public class BookPresenter implements BookContract.Presenter {

    private BookApi mApi;
    private BookContract.View mView;


    @Inject
    public BookPresenter() {
        mApi = new BookApi();
    }

    @Override
    public void fetBookDetail(String id) {
        mApi.fetBookDetailById(id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BookDetailEntity>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(BookDetailEntity entity) {
                        mView.displayBookDetail(entity);
                    }
                });
    }


    @Override
    public void attachView(@NonNull BookContract.View view) {
        this.mView = view;
    }

    @Override
    public void detachView() {
        this.mView = null;
    }
}
